import Vue from 'vue'
import { sessionStore } from 'store/dist/store.legacy'
const state = {
  listUrl: process.env.VUE_APP_specialList_url,
  showUrl: process.env.VUE_APP_specialShow_url,
  limit: 50,
  list: [],
  show: [],
  special: []
}

const mutations = {
  setList (state, param) {
    state.list = param.data
    sessionStore.set('specialList', param.data)
  },
  setShow (state, param) {
    state.show = param.data
    sessionStore.set(`specialShow_${param.id}`, param.data)
  },
  setSpecial (state, param) {
    Vue.set(state, 'special', param.data)
  }
}

const actions = {
  setList ({ state, commit }, param) {
    if (state.list.length > 0) {
      return
    }
    let list = sessionStore.get('specialList')
    if (!list) {
      let url = state.listUrl
      window.axios.post(url, {
        limit: state.limit,
      }).then((ret) => {
        commit('setList', { data: ret.data })
      })
    } else {
      commit('setList', { data: list })
    }
  },
  setShow ({ state, commit }, param) {
    let show = sessionStore.get(`specialShow_${param.id}`)
    if (!show) {
      let url = state.showUrl
      window.axios.post(url, { id: param.id }).then((ret) => {
        commit('setShow', { data: ret.data, id: param.id, width: 140, height: 90 })
      })
    } else {
      commit('setShow', { data: show, id: param.id })
    }
  },
  setSpecial ({ state, commit }, param) {
    let list = sessionStore.get('specialList')
    let special = undefined
    if (list) {
      special = list.find(item => item.id == param.id)
    }
    if (special == undefined) {
      let url = state.listUrl
      window.axios.post(url, {
        limit: state.limit,
      }).then((ret) => {
        commit('setList', { data: ret.data })
        commit('setSpecial', { data: ret.data.find(item => item.id == param.id) })
      })
    } else {
      commit('setSpecial', { data: special })
    }


  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}